package com.winning.pmph.ctrl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.winning.pmph.entity.ExamOutline;
import com.winning.pmph.entity.ExamOutlineDetail;
import com.winning.pmph.entity.ExamOutlineTag;
import com.winning.pmph.service.ExamOutlineService;
import com.winning.pmph.utils.*;
import com.winning.pmph.vo.ExamOutlineParamVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author admin
 */
@Api(value = "考试大纲")
@RestController
@RequestMapping("/pmph/action/examOutline")
public class ExamOutlineCtrl {

    @Resource
    private ExamOutlineService examOutlineService;

    @ApiOperation(value = "uploadAndInsertFile", notes = "考试大纲文件上传")
    @PostMapping("uploadAndInsertFile")
    public void uploadAndInsertFile(MultipartFile file, String id) {
        String path = ApplicationContextUtil.BASE_DOWN_FORDER + "file/examOutline/";
        ExamOutline examOutline = examOutlineService.getById(id);
        String fileName = UploadFileUtil.uploadFile(file, path, false);
        if (Objects.isNull(examOutline)) {
            examOutline = new ExamOutline();
            examOutline.setId(IdWorker.getIdStr());
            examOutline.setCreatedBy(PMPHAppUtil.getCurrentUserEnName());
        }
        examOutline.setUpdatedBy(PMPHAppUtil.getCurrentUserEnName());
        examOutline.setName(fileName);
        examOutline.setImportStatus(Const.IMPORT_STATUS_IMPORTING);
        examOutlineService.saveOrUpdate(examOutline);
        ExamOutline finalExamOutline = examOutline;
        ExecutorServiceFactory.getInstance().submit(() -> {
            try {
                examOutlineService.uploadAndInsertFile(path + fileName, finalExamOutline);
            } catch (RuntimeException e) {
                finalExamOutline.setImportStatus(Const.IMPORT_STATUS_FAILURE);
                finalExamOutline.setFailureReason(e.toString());
                throw new RuntimeException(e.toString());
            } finally {
                examOutlineService.saveOrUpdate(finalExamOutline);
            }
        });
    }

    @ApiOperation(value = "selectPage", notes = "查询大纲列表")
    @GetMapping("selectPage")
    public Page<ExamOutline> selectPage(ExamOutline examOutline) {
        LambdaQueryChainWrapper<ExamOutline> queryChainWrapper = examOutlineService.lambdaQuery()
                .like(StringUtils.isNotBlank(examOutline.getName()), ExamOutline::getName, examOutline.getName())
                .like(StringUtils.isNotBlank(examOutline.getMajor()), ExamOutline::getMajor, examOutline.getMajor())
                .eq(StringUtils.isNotBlank(examOutline.getCreatedBy()), ExamOutline::getCreatedBy, examOutline.getCreatedBy())
                .eq(Objects.nonNull(examOutline.getImportStatus()), ExamOutline::getImportStatus, examOutline.getImportStatus())
                .orderByDesc(ExamOutline::getCreatedTime).orderByAsc(ExamOutline::getId);
        if (CollectionUtils.isNotEmpty(examOutline.getImportDateRange())) {
            queryChainWrapper.ge(ExamOutline::getCreatedTime, examOutline.getImportDateRange().get(0));
            queryChainWrapper.le(ExamOutline::getCreatedTime, examOutline.getImportDateRange().get(1));
        }
        if (Objects.nonNull(examOutline.getUnitNumStartIndex())) {
            queryChainWrapper.ge(ExamOutline::getUnitNum, examOutline.getUnitNumStartIndex());
            queryChainWrapper.le(ExamOutline::getUnitNum, examOutline.getUnitNumEndIndex());
        }
        if (Objects.nonNull(examOutline.getTagNumStartIndex())) {
            queryChainWrapper.ge(ExamOutline::getTagNum, examOutline.getTagNumStartIndex());
            queryChainWrapper.le(ExamOutline::getTagNum, examOutline.getTagNumEndIndex());
        }
        if (Objects.nonNull(examOutline.getDetailNumStartIndex())) {
            queryChainWrapper.ge(ExamOutline::getDetailNum, examOutline.getDetailNumStartIndex());
            queryChainWrapper.le(ExamOutline::getDetailNum, examOutline.getDetailNumEndIndex());
        }
        if (Objects.nonNull(examOutline.getKnowledgeNumStartIndex())) {
            queryChainWrapper.ge(ExamOutline::getKnowledgeNum, examOutline.getKnowledgeNumStartIndex());
            queryChainWrapper.le(ExamOutline::getKnowledgeNum, examOutline.getKnowledgeNumEndIndex());
        }
        if (Objects.nonNull(examOutline.getExerciseNumStartIndex())) {
            queryChainWrapper.ge(ExamOutline::getExerciseNum, examOutline.getExerciseNumStartIndex());
            queryChainWrapper.le(ExamOutline::getExerciseNum, examOutline.getExerciseNumEndIndex());
        }
        return queryChainWrapper.page(new Page<>(examOutline.getCurrentPage(), examOutline.getPageSize()));
    }

    @ApiOperation(value = "deleteById", notes = "根据ID删除大纲")
    @GetMapping("deleteById")
    public void deleteById(String id) {
        examOutlineService.deleteById(Arrays.asList(id.split(",")));
    }

    @ApiOperation(value = "selectExamOutlineById", notes = "根据ID查询大纲基本信息")
    @GetMapping("selectExamOutlineById")
    public ExamOutline selectExamOutlineById(String id) {
        return examOutlineService.getById(id);
    }

    @ApiOperation(value = "updateById", notes = "根据ID更新大纲基本信息")
    @PostMapping("updateById")
    public void updateById(@RequestBody ExamOutline examOutline) {
        examOutlineService.updateById(examOutline);
    }

    @ApiOperation(value = "selectAllTagList", notes = "根据ID查询大纲所有分类")
    @GetMapping("selectAllTagList")
    public List<ExamOutlineTag> selectAllTagList(String examOutlineId) {
        return examOutlineService.selectAllTagList(examOutlineId);
    }

    @ApiOperation(value = "saveTag", notes = "新增大纲分类")
    @PostMapping("saveTag")
    public void saveTag(@RequestBody ExamOutlineTag examOutlineTag) {
        examOutlineService.saveTag(examOutlineTag);
    }

    @ApiOperation(value = "deleteTag", notes = "删除分类标签及子标签")
    @PostMapping("deleteTag")
    public void deleteTag(@RequestBody ExamOutlineTag examOutlineTag) {
        examOutlineService.deleteTag(examOutlineTag);
    }

    @ApiOperation(value = "tagMove", notes = "分类标签上移/下移")
    @PostMapping("tagMove")
    public void tagMove(@RequestBody ExamOutlineParamVo tagMoveUp) {
        examOutlineService.tagMove(tagMoveUp);
    }

    @ApiOperation(value = "selectDetailPage", notes = "根据大纲ID查询要点明细,分类是1级时," +
            "查询所有要点,非1级时,只查询当前标签下对应的要点,方便对要点进行上下移")
    @GetMapping("selectDetailPage")
    public Page<ExamOutlineDetail> selectDetailPage(ExamOutlineDetail examOutlineDetail) {
        return examOutlineService.selectDetailPage(examOutlineDetail);
    }

    @ApiOperation(value = "selectDetailName", notes = "根据大纲ID,分类ID查询和要点名查询要点名是否已存在")
    @GetMapping("selectDetailName")
    public int selectDetailName(ExamOutlineDetail examOutlineDetail) {
        return examOutlineService.selectDetailName(examOutlineDetail);
    }

    @ApiOperation(value = "saveDetail", notes = "新增/编辑要点")
    @PostMapping("saveDetail")
    public void saveDetail(@RequestBody ExamOutlineDetail examOutlineDetail) {
        examOutlineService.saveDetail(examOutlineDetail);
    }

    @ApiOperation(value = "deleteDetailById", notes = "删除要点")
    @GetMapping("deleteDetailById")
    public void deleteDetailById(String id, String examOutlineTagId, String examOutlineId) {
        examOutlineService.deleteDetailById(Arrays.asList(id.split(",")), examOutlineTagId, examOutlineId);
    }

    @ApiOperation(value = "detailMove", notes = "要点上移/下移")
    @PostMapping("detailMove")
    public void detailMove(@RequestBody ExamOutlineParamVo examOutlineParamVo) {
        examOutlineService.detailMove(examOutlineParamVo);
    }
}
